Conversation
omochi
commented
Jan 1, 2025
| return name | ||
| } | ||
| i += 1 | ||
| } |
Owner
Author
There was a problem hiding this comment.
2から始めて衝突しなくなるまでインクリメントし続ける。
| .callDecodeField(json: expr) | ||
|
|
||
| let varName = nameProvider.provide(base: TSKeyword.escaped(label)) | ||
| names[label] = varName |
Owner
Author
There was a problem hiding this comment.
用意された名前は後で参照する時のために辞書に入れておく
| ) throws -> TSObjectExpr { | ||
| return TSObjectExpr(try caseElement.associatedValues.map { (value) in | ||
| let label = value.codableLabel | ||
| let varName = try names[label].unwrap(name: "var name") |
Owner
Author
There was a problem hiding this comment.
用意した名前を辞書から引く。
エスケープ済みの名前を格納してるので利用側のエスケープ処理が不要になっている。
| ) | ||
| ) | ||
| block.append(j) | ||
| nameProvider.register(name: "j") |
Owner
Author
There was a problem hiding this comment.
enumについては e や j という変数も使用するが、
これとも衝突しないように利用済みの名前を追記しておく
| guard let decl = try converter.decodeSignature() else { return nil } | ||
|
|
||
| var nameProvider = NameProvider() | ||
| nameProvider.register(signature: decl) |
Owner
Author
There was a problem hiding this comment.
関数の仮引数名を登録しておく。
これは関数の実装部全体で有効なのでこのnameProviderをこの先で引き継ぐが、
ローカルスコープ同士の間では名前の衝突は起きない。
そこはうまく局所的な生成関数に nameProvider をコピー渡しすることで、隣のスコープ同士が影響しないようにしつつ、関数仮引数という上流のスコープの影響は引き継がせる。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
トランスパイルするSwiftの型が
var entityのようなプロパティを持っていると、コード生成するときにJSの関数の引数の
entityと、プロパティに対応して生成されるローカル変数の
const entityが衝突して、TypeScriptとしてコンパイル不可能になってしまう。
このパッチはこの問題を回避する。
ローカル変数を生成するときに既存の名前として使用済みかどうかを調べて、
使用済みであれば代わりに連番をつけた名前を使用する。